package my.jwds.p6023;

public class MinimumWhiteTiles {

    public static void main(String[] args) {

        MinimumWhiteTiles obj = new MinimumWhiteTiles();
        long start = System.currentTimeMillis();
        System.out.println(obj.minimumWhiteTiles(
                "1101"
                ,3
                ,1));
        System.out.println("运行时间:" + (System.currentTimeMillis() - start));
    }

    public int minimumWhiteTiles(String floor, int numCarpets, int carpetLen) {
        if (numCarpets * carpetLen >= floor.length()) return 0;
        int white = 0,n = floor.length();
        int[]prefixWhiteSum=new int[n];
        for (int i = 0; i < n; i++) {
            white += floor.charAt(i) - '0';
            prefixWhiteSum[i] = white;
        }
        int[][]dp = new int[numCarpets+1][n];
        for (int i = 1; i <= numCarpets; i++) {
            dp[i][carpetLen-1] = prefixWhiteSum[carpetLen-1] ;
            for (int j = carpetLen; j < n; j++) {
                dp[i][j] = Integer.max(dp[i][j-1],dp[i-1][j-carpetLen] + prefixWhiteSum[j] - prefixWhiteSum[j-carpetLen]);
            }
        }
        return white - dp[numCarpets][n-1];
    }
}